//
//  ChartFileHelper.m
//  Pods
//
//  Created by E.F.B on 15-5-5.
//
//

#import "ChartFileHelper.h"

@implementation ChartFileHelper

+ (NSString *)pathForChartsDB:(NSString *)revisionID
{
    NSArray * dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * cachesPath = [dirs objectAtIndex:0];
    NSString * storagePath = [cachesPath stringByAppendingPathComponent:@"charts"];
    storagePath=[storagePath stringByAppendingPathComponent:revisionID];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:storagePath]) {
        return [NSString stringWithFormat:@"%@/tc.db3",storagePath];
    }
    return [NSString stringWithFormat:@"%@/tc.db3",storagePath];
}

+ (NSString *)pathForChartsPlist:(NSString *)revisionID userName:(NSString *)name
{
    static NSString * rootDir = @"charts";
    
    NSArray * dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * cachesPath = [dirs objectAtIndex:0];
    NSString * storagePath = [cachesPath stringByAppendingPathComponent:rootDir];
    
    storagePath=[storagePath stringByAppendingPathComponent:revisionID];
    
    if (name.length>0) {
        
        storagePath=[storagePath stringByAppendingPathComponent:name];
        
        NSFileManager *manager=[NSFileManager defaultManager];
        if (![manager fileExistsAtPath:storagePath]) {
            [manager createDirectoryAtPath:storagePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    
    return storagePath;
}

+ (NSString*)pathForCharts:(NSString *)task_id
{
    NSString* fileName = @"";
    if (task_id.length > 0) {
        fileName = [NSString stringWithFormat:@"/%@.zip", task_id];
    }
    
    NSString* folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/charts/"];
    
    NSFileManager *_manager=[NSFileManager defaultManager];
    if (![_manager fileExistsAtPath:folderPath]) {
        [_manager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return [folderPath stringByAppendingString:fileName];
}

+ (NSString*)pathForChartsPlist
{
    NSString* folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/charts/"];
    
    NSFileManager *_manager=[NSFileManager defaultManager];
    if (![_manager fileExistsAtPath:folderPath]) {
        [_manager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return [folderPath stringByAppendingString:@"/chartsPage.plist"];
}

#pragma mark --系统日志文件
+ (NSString *)pathForTextContents
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirect = [paths objectAtIndex:0];
    NSString * fullPath = [documentsDirect stringByAppendingPathComponent:@"ChartsLog.txt"];
    return fullPath;
}

+ (void)writeChartsLogToFile:(NSString *)detailMessage
{
    NSString *strMessageAll=@"";
    NSString *userName=@"*****航图*****";
    
    NSDateFormatter *formate=[[[NSDateFormatter alloc]init]autorelease];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *timeNow=[formate stringFromDate:[NSDate date]];
    
    NSString *filePath=[ChartFileHelper pathForTextContents];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        if (userName==nil)
        {
            strMessageAll=[NSString stringWithFormat:@"\r%@,,%@",timeNow,detailMessage];
        }else
        {
            strMessageAll=[NSString stringWithFormat:@"\r%@,%@,%@",timeNow,userName,detailMessage];
        }
        
        NSFileHandle *fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:filePath];
        [fileHandle seekToEndOfFile];
        NSData *stringData=[strMessageAll dataUsingEncoding:NSUTF8StringEncoding];
        [fileHandle writeData:stringData];
        [fileHandle closeFile];
    }else
    {
        if (userName==nil)
        {
            strMessageAll=[NSString stringWithFormat:@"%@,,%@",timeNow,detailMessage];
        }else
        {
            strMessageAll=[NSString stringWithFormat:@"%@,%@,%@",timeNow,userName,detailMessage];
        }
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:[strMessageAll dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    }
}

+ (NSString *)pathForCOCharts:(NSString *)uuid
{
    NSString* folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/charts/"];
    NSFileManager *_manager=[NSFileManager defaultManager];
    if (![_manager fileExistsAtPath:folderPath]) {
        [_manager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return [folderPath stringByAppendingString:[NSString stringWithFormat:@"/%@/CoData.plist",uuid]];
}
@end
